#pragma once
#include <string>
#include <ostream>

namespace krryn{
namespace core{
	// A (trivial) string class that doesn't own it's own data and
	// is basically a substring of a larger string buffer. This
	// can be used to eliminate string copies by simply sharing
	// the buffer. This also means that owned_string doesn't
	// handle anything related to the lifetime of it's m_Data
	// pointer.
	class owned_string{
	public:
		char *m_Data;
		size_t m_Length;

		typedef size_t size_type;
		typedef char * const_iterator;

		owned_string();
		owned_string(char *a_Data, size_t a_Length);

		inline char *begin() const { return m_Data; }
		inline char *end() const { return m_Data + m_Length; }
		inline size_t length() const { return m_Length; }
		inline bool empty() const { return m_Length == 0; }

		inline char &operator[](size_t a_Index){
			return m_Data[a_Index];
		}

		inline bool operator==(const owned_string &a_Other){
			return !strcmp(m_Data, a_Other.m_Data, m_Length, a_Other.m_Length);
		}

		inline bool operator!=(const owned_string &a_Other){
			return !this->operator==(a_Other);
		}

		inline bool operator==(const char *a_Str){
			return !strcmp(m_Data, a_Str, m_Length);
		}

		inline bool operator!=(const char *a_Str){
			return !this->operator==(a_Str);
		}

		owned_string trim(char l_ToTrim = ' ');

		// Note: this function assumes that a_First isn't null terminated
		// (eg is data that stems from an owned_string instance) hence the
		// a_FirstLength parameter. However:
		//   this function does assume that the second parameter /is/ null terminated!
		// Make sure you supply the correct arguments.
		bool strcmp(char *a_First, const char *a_SecondNullTerminated, size_t a_FirstLength) const;

		bool strcmp(char *a_First, const char *a_Second, size_t a_FirstLength, size_t a_SecondLength) const;
	};

	std::ostream &operator<<(std::ostream &a_Out, const owned_string &a_Str);
	bool operator==(const std::string &a_Other, const owned_string &a_Str);
}
}